package fr.efrei.tradesys.struts.actions.currencies;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fr.efrei.tradesys.hibernate.beans.AppUser;
import fr.efrei.tradesys.hibernate.beans.Currency;
import fr.efrei.tradesys.hibernate.beans.Owned;
import fr.efrei.tradesys.hibernate.beans.Rate;
import fr.efrei.tradesys.hibernate.factory.DaoFactory;

public class DisplayRateDetailAction extends ActionSupport
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private static Log				LOGGER						= LogFactory
																									.getLog(DisplayRateDetailAction.class);
	private String						code1;
	private String						code2;
	private Currency					currency1;
	private Currency					currency2;
	private Owned							o1;
	private Owned							o2;
	private Rate							rate;
	private List<Rate>				oldRates;

	public String execute() throws Exception
	{
		LOGGER.info("Execution of " + this.getClass().getName());
		Map<String, Object> session = ActionContext.getContext().getSession();
		AppUser u = (AppUser) session.get("user");
		currency1 = DaoFactory.getInstance().getCurrencyDao().getCurrency(code1);
		currency2 = DaoFactory.getInstance().getCurrencyDao().getCurrency(code2);
		o1 = DaoFactory.getInstance().getOwnedDao().getOwned(u, currency1);
		if (o1 == null)
		{
			o1 = new Owned(null, null, 0);
		}
		o2 = DaoFactory.getInstance().getOwnedDao().getOwned(u, currency2);
		if (o2 == null)
		{
			o2 = new Owned(null, null, 0);
		}
		oldRates = DaoFactory.getInstance().getRateDao()
				.getRates(currency1, currency2);
		if (oldRates != null && oldRates.size() > 0)
		{
			rate = oldRates.get(0);
		}
		return SUCCESS;
	}

	public String getCode1()
	{
		return code1;
	}

	public void setCode1(String code1)
	{
		this.code1 = code1;
	}

	public String getCode2()
	{
		return code2;
	}

	public void setCode2(String code2)
	{
		this.code2 = code2;
	}

	public Currency getCurrency1()
	{
		return currency1;
	}

	public void setCurrency1(Currency currency1)
	{
		this.currency1 = currency1;
	}

	public Currency getCurrency2()
	{
		return currency2;
	}

	public void setCurrency2(Currency currency2)
	{
		this.currency2 = currency2;
	}

	public Owned getO1()
	{
		return o1;
	}

	public void setO1(Owned o1)
	{
		this.o1 = o1;
	}

	public Owned getO2()
	{
		return o2;
	}

	public void setO2(Owned o2)
	{
		this.o2 = o2;
	}

	public Rate getRate()
	{
		return rate;
	}

	public void setRate(Rate rate)
	{
		this.rate = rate;
	}

	public List<Rate> getOldRates()
	{
		return oldRates;
	}

	public void setOldRates(List<Rate> oldRates)
	{
		this.oldRates = oldRates;
	}

}
